package hasthi.actuators;

import static hasthi.common.constants.WsrfConstants.WSA_ACTION_UPDATE_RESOURCE_PROPERTY;
import hasthi.common.HasthiException;
import hasthi.common.Utils;
import hasthi.common.XmlBeanUtils;
import hasthi.container.MessageContext;
import hasthi.container.SimpleMessageContext;
import hasthi.metamodel.resource.ManagedResource;

import javax.xml.namespace.QName;

import wsrf.UpdateResourcePropertiesDocument;
import wsrf.UpdateType;

public class SetPropertyAction extends ResourceBasedAction{
    private QName propertyName;
    private String value;
    public SetPropertyAction(ManagedResource resource,String propertyName, String value) {
        super(resource);
        this.propertyName = Utils.getPropertyQNameByName(propertyName);
        this.value = value;
    }
    
    public SetPropertyAction(ManagedResource resource,QName propertyName, String value) {
        super(resource);
        this.propertyName = propertyName;
        this.value = value;
    }
    
    public String getActionID() {
        return this.getClass().toString() + "|" + resource.getName() + "|" + propertyName + "=" + value;
    }
    
    public void execute() throws HasthiException {
        UpdateResourcePropertiesDocument setRPDocument = UpdateResourcePropertiesDocument.Factory.newInstance();
        UpdateType propertyData = setRPDocument.addNewUpdateResourceProperties().addNewUpdate();
        XmlBeanUtils.addNameValuePair(propertyData, propertyName, value);
        MessageContext message = new SimpleMessageContext(setRPDocument,resource.getManagerAgentURL(),
                WSA_ACTION_UPDATE_RESOURCE_PROPERTY);
        XmlBeanUtils.prettyPrint(actionContext.getUnicastContainer().sendReceive(message));
    }

    public QName getPropertyName() {
        return propertyName;
    }
}
